import itertools
import json
import os
def show():
    print("show")

def str2(i):
    return format(i, '02d')

dict_items=None
#生成字典
def get_ssq_all():   
    global dict_items
    if os.path.exists('./ssq_all.json') and dict_items==None:
        with open('./ssq_all.json') as file:
            dict_items=json.load(file)
        return dict_items
    # 生成红色球号码的范围（1-33）
    red_balls = list(range(1, 34))

    # 生成蓝色球号码的范围（1-16）
    blue_balls = list(range(1, 17))

    # 使用itertools模块的combinations函数生成所有可能的红色球组合
    red_combinations = list(itertools.combinations(red_balls, 6))

    # 生成所有红蓝球号码的组合
    all_combinations = []
    for red_combination in red_combinations:
        for blue_ball in blue_balls:
            all_combinations.append(red_combination + (blue_ball,))
    # 打印所有双色球号码的组合
    
    for i, combination in enumerate(all_combinations, 1): 
        str_ssq=','.join(map(str2, combination))
        dict_items[str_ssq]=i-1
        
    with open('./ssq_all.json','w') as file:
        json.dump(dict_items, file)
    return dict_items

def get_windex_to_w(w_index):
    dict_items=get_ssq_all()
    for key, value in dict_items.items():
        if value == w_index:           
            return key

# 数据集中获取所有历史数据
def get_ssq_db():
    lst=[]
    with open('./ssq_all.json') as file:
        lst=json.load(file)          
    return lst

# #根据数据库中的id获取4条数据
# def get_ssq_lishi_by_qihao(id,size=4):
#     # 建立数据库连接
#     conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', db='spider_db')
#     # 创建游标对象
#     cursor = conn.cursor()
#     # 执行 SQL 查询语句
#     sql = f"SELECT kaijianghaoma,w_index,id FROM ssq_info WHERE qihao <= '{id}' ORDER BY qihao DESC LIMIT 4;"
#     cursor.execute(sql)
#     # 获取查询结果
#     result = cursor.fetchall()
#     lst=[]
#     for row in result:      
#         w_index=row[1]
#         lst.append(w_index)       
#     # 关闭游标和数据库连接
#     cursor.close()
#     conn.close()
#     #重新升序排列
#     lst.reverse()
#     return lst
